package com.zjm.service.impl;

import com.zjm.dao.*;
import com.zjm.entity.*;
import com.zjm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class StudentServiceImpl implements StudentService {

    //自动注入
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private VacateDao vacateDao;

    @Autowired
    private MajorsDao majorsDao;

    @Autowired
    private ClasDao clasDao;

    @Autowired
    private ChangeMajorDao changeMajorDao;

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public Map<String ,Object> register(Student student,String confirmPassword) {
        Map<String,Object> map = new HashMap<>();
        if(student.getId()==null){
            map.put("msg","账号不能为空");
            return  map;
        }
        if(student.getPassword()==null){
            map.put("msg","密码不能为空");
            return  map;
        }
        if(confirmPassword==null){
            map.put("msg","确认密码不能为空");
            return  map;
        }
        if(!student.getPassword().equals(confirmPassword)){
            map.put("msg","两次密码不相同");
            return  map;
        }
        if(student.getName()==null){
            map.put("msg","姓名不能为空");
            return  map;
        }
        if(student.getAcademy()==null){
            map.put("msg","学院不能为空");
            return  map;
        }
        if(student.getMajor()==null){
            map.put("msg","专业不能为空");
            return  map;
        }
        if(student.getClas()==null){
            map.put("msg","班级不能为空");
            return  map;
        }
        Student stu = studentDao.selectStudentById(student.getId());
        if(stu!=null){
            map.put("msg","用户已存在");
            return map;
        }
        int num = studentDao.insertStudent(student);
        return map;
    }

    @Override
    public Student login(Integer id,String password) {
        if (id>-1 && password !=null){
            Student student = studentDao.selectStudentById(id);
            if(student.getPassword().equals(password))
                return student;
        }
        return null;
    }

    @Override
    public Student showInfo(Integer student_id) {
        return null;
    }

    @Override
    public List<Vacate> myVacates(Integer student_id) {
        List<Vacate> vacateList = vacateDao.selectStudentVacate(student_id);
        return vacateList;
    }

    @Override
    public Map<String, String> vacate(Vacate vacate) {
            vacateDao.insertVacate(vacate);
        return null;
    }

    @Override
    public Change_major myChangeMajor(Integer student_id) {
        Change_major changeMajorList = changeMajorDao.selectChangeMajor(student_id);
        return changeMajorList;
    }

    @Override
    public Map<String, String> changeMajor(Change_major change_major) {

        if(changeMajorDao.selectChangeMajor(change_major.getStudent_id()) == null) {
            changeMajorDao.insertChangeMajor(change_major);
        }else if(changeMajorDao.selectOnChangeMajor(change_major.getStudent_id()) == null){
            changeMajorDao.insertupdateChangeMajor(change_major);
        } else{
            System.out.println("已经在转专业了");
        }
        return null;
    }

    @Override
    public List<Select_clas> myClas(Integer student_id) {
        System.out.println("====="+student_id);
        List<Select_clas> clasList = clasDao.SelectStudentSelectedClas(student_id);
        return clasList;
    }

    @Override
    public List<Clas> showAllClas(Integer student_id) {
        //List<Clas> clasList = clasDao.selectAllClas();
        List<Clas> clasList = clasDao.selectClas(student_id);
        return clasList;
    }

    @Override
    public Map<String, String> addClas(Integer clas_id,Integer student_id) {
        Clas clas = clasDao.selectOneClas(clas_id);
        Student student = studentDao.selectStudentById(student_id);
        Teacher teacher = teacherDao.selectTeacherById(clas.getTeacher_id());
        System.out.println(clas);

        if(clas == null || student == null ||teacher == null || clas_id == null || clas_id == null || clas.getSelected()==null||clas.getAmount()==null){

        }else{
            if(clas.getSelected()<clas.getAmount()){
                Select_clas select_clas = new Select_clas();
                select_clas.setClas_id(clas_id);
                select_clas.setClas_name(clas.getClas_name());
                select_clas.setClas_time(clas.getClas_time());
                select_clas.setStudent_id(student_id);
                select_clas.setStudent_name(student.getName());
                select_clas.setTeacher_id(teacher.getId());
                select_clas.setTeacher_name(teacher.getName());

                Integer selected = clas.getSelected()+1;

                clasDao.insertSelectClas(select_clas);
                clasDao.addClasNum(selected,clas_id);
            }
        }
        return null;
    }

}
